#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :话题.py
# @Time      :2024/11/27 
# @Author    :CL
# @email     :1037654919@qq.com
# 获取话题热度信息
from scrapy_juliangsuanshu import *
def get_topics_list(keywords,app_names):   #获取相关话题的topic_id
    print ('begin function:get_topics_list')
    for keyword in keywords:
        print('begin:',keyword)
        for app_name in app_names:
            datayy = pd.DataFrame()
            try:
                data = get_data5(keyword, app_name)
                for topic in data['topic_list']:
                    datayy = datayy.append(
                        [{'keyword': keyword, 'topic_name': topic['topic_name'],
                          'topic_id': topic['topic_id'],'app_name':app_name}],
                        ignore_index=True)
                # datayy.to_sql('topics_list', engine, if_exists='append', index=False, )
                print('topics_list of %s complete in %s'%(keyword,app_name))
            except:
                print('something wrong with  %s %s'%(keyword,app_name))
                pass
        time.sleep(5)
    return

def get_multi_topic_list(begin_date,end_date):
    topics_info = pd.read_sql_table(table_name='topics_list', con=engine)
    for topic in topics_info.itertuples():
        print(topic[2],topic[3])
        topic_id =topic[3]
        for app_name in app_names:
            try:
                datastr = get_data6(topic_ids=topic_id, app_name=app_name, begin_date=begin_date, end_date=end_date)
                data = decrtptlx(datastr)
                print(data[0:100])
                print('%s in %s success'%(topic[1],app_name))
            except:print('%s in %s fialed'%(topic[1],app_name))
    return
